// pages/conponscenter/conponscenter.js
const myRequest = require('../../assets/js/request.js');
const api = require('../../assets/js/api.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typelist: [{
      type: "全部",
      number: 2
    }, {
      type: "满减券",
      number: 0
    }, {
      type: "通用券",
      number: 1
    }],
    conponslist: [],
    activeIndex: 0,
    type:2,//全部优惠券信息
    money:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getdata(this.data.type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  //筛选优惠券
  showtype: function(e) {
    let that = this
    let type = e.currentTarget.dataset.type
    this.getdata(type)
    that.setData({
      activeIndex: e.currentTarget.dataset.index
    })
  },

  //接口请求类型数据
  getdata: function(a) {
    let that = this
    myRequest._get(api.couponslist + '?type=' + a).then((res) => {
      console.log(res)
      that.setData({
        conponslist: res.data.goodslist
      })
    })
  },
  //接口请求价格搜索数据
  getdataA: function (a) {
    let that = this
    myRequest._get(api.couponslist + '?price=' + a).then((res) => {
      console.log(res)
      that.setData({
        conponslist: res.data.goodslist
      })
    })
  },
  //领取优惠券
  getcoupons:function(e){
    if (app.globalData.userInfo == null) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '确定',
        cancelText: '取消',
        success: function (res) {
          if (res.confirm) {
            wx.setStorage({
              key: 'isBack',
              data: 1,
            })
            wx.getStorage({
              key: 'accreditInfo',
              success: function (res) {
                wx.navigateTo({
                  url: '/pages/setting/setting',
                })
              },
              fail: function () {
                wx.switchTab({
                  url: '/pages/personal/personal',
                })
              }
            })
          } else if (res.cancel) {
            return;
          }
        }
      })
    }
    else {
      let arr = []
      wx.getStorage({
        key: 'getyhlist',
        success: function (res) {
          arr = res.data
          arr.push(e.currentTarget.dataset.detail)
        },
        fail: function () {
          arr.push(e.currentTarget.dataset.detail)
        },
        complete: function () {
          wx.setStorage({
            key: 'getyhlist',
            data: arr,
          })
          wx.showToast({
            title: '领取成功',
            icon: 'none',
            duration: 1000
          })
        }
      })
    }
  },
  //输入金额
  write:function(e){
    console.log(e.detail.value)
    this.setData({
      money: e.detail.value
    })
  },
  //搜索优惠券
  search:function(){
    let that = this.data.money
    console.log(that)
    this.getdataA(that)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})